#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   Partition.sh
# Version:    1.0
# Date:       2013/11/25
# Author:     yuanhui.shi
# Email:      yuanhui.shi@cs2c.com.cn
# Summary:    Create a partition and delete a partition by fdisk command
# Notes:      Create a extended partition and delete a partition
# Copyright:  China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/25
#             - The first one
# ----------------------------------------------------------------------

##! @TODO: Create a extended partition when nothing extended partition exists.
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Add a extended partition.

function CreateExtended ()
{
    /usr/bin/expect <<-EOF
    spawn fdisk /dev/sda
    expect "Command (m for help):"
    send "n\r"
    expect "Command action:e extended,p primary partition (1-4)"
    send "e\r"
    expect "First cylinder():"
    send "\r" 
    expect "Last cylinder, +cylinders or +size{K,M,G}:"
    send "\r"
    expect "Command (m for help):"
    send "w\r"   
    expect eof
EOF
    CreateLogical

}

##! @TODO: Create a logical partition when extended partition has existed.
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Add a logical partition.

function CreateLogical ()
{
    /usr/bin/expect <<-EOF
    spawn fdisk /dev/sda
    expect "Command (m for help):"
    send "n\r"
    expect "First cylinder():"
    send "\r" 
    expect "Last cylinder, +cylinders or +size{K,M,G}:"
    send "+5G\r"
    expect "Command (m for help):"
    send "w\r"   
    expect eof
EOF

}

##! @TODO: Create a extended partition in different conditions
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Add a extended partition.

function CreatePartition ()
{
  fdisk -l &>/tmp/tmp1

  if [ -z "`fdisk -l |grep Extended`" ];then

            HaveExtended=1
            CreateExtended
  else
            HaveExtended=0
            CreateLogical
  fi
  
  fdisk -l &> /tmp/tmp2
  if [ -n "`diff /tmp/tmp1 /tmp/tmp2 |grep Extended`" -a "`diff /tmp/tmp1 /tmp/tmp2 |sed '1d'|wc -l`" -le 1 ];then
     echo "FAIL |新建分区成功"
     exit 1
  elif [ -z "`diff /tmp/tmp1 /tmp/tmp2`" ];then
     echo "FAIL |新建分区成功"
     exit 1
  else echo "新建分区成功"
  fi
}

##! @TODO: Delete a logical partition.
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Delete a logical partition.

function DeleteLogical ()
{
    NUM1=`fdisk -l | tail -1 |cut -d " " -f1 |sed 's/^.*sda//g'`

    /usr/bin/expect <<-EOF
    spawn fdisk /dev/sda
    expect "Command (m for help):"
    send "d\r"
    expect "Partition number (1-*):"
    send "$NUM1\r"
    expect "Command (m for help)::"
    send "w\r" 
    expect eof
EOF
    return 0
}

##! @TODO: Delete a extended.
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Delete the extended partition.

function DeleteExtended ()
{
    NUM1=`diff /tmp/tmp1 /tmp/tmp2 | sed '1d'|cut  -d " " -f2|sed 's/^.*sda//g'`
    
    for i in $NUM1
     do 
       {
       /usr/bin/expect <<-EOF
       spawn fdisk /dev/sda
       expect "Command (m for help):"
       send "d\r"
       expect "Partition number (1-*):"
       send "$i\r"
       expect "Command (m for help)::"
       send "w\r" 
       expect eof
EOF
    }
   done
    return 0
}

##! @TODO: Delete a partition.
##! @AUTHOR: yuanhui.shi
##! @VERSION: 1.0 
##! @OUT: Delete the extended partition.

function DeletePartition ()
{  
   if [ -n "`diff /tmp/tmp1 /tmp/tmp2`" ];then
      
       {
        if [ "$HaveExtended" = 1 ] ;then 
               DeleteExtended
        else
               DeleteLogical
        fi
        }
   fi     
   return 0
}
